from django.shortcuts import render,HttpResponse
from . models import *
from .forms import *
import requests
from django import views


# Create your views here.
#登录
def denglu(request):
    username=request.POST.get("username")
    password=request.POST.get("password")
    if Add.objects.filter(username=username,password=password):
        return render(request,'yonghu.html')
    return render(request,'denglu.html')

#注册
def zhuce(request):
    if request.method.lower()=="post":
        username=request.POST.get("username")
        password=request.POST.get("password")
        Add.objects.create(username=username,password=password)
        return render(request,"denglu.html")
    return render(request,"zhuce.html")


def index():
    url = 'https://aip.baidubce.com/oauth/2.0/token?' \
          'grant_type=client_credentials&' \
          'client_id=hX6neQCnWEK5KeRXTqFPilRg&' \
          'client_secret=oCTzAc8D3OpE7QNs2bzD8pGx8TTXGtXY'
    r = requests.post(url=url)
    access_token = r.json().get('access_token')
    return access_token

#人脸识别
def shouye(request):
    if request.method == "GET":
        return render(request,'shouye.html')
    else:
        pic = request.POST.get('pic')
        print(type(pic))
        print(pic)


        url = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?' \
              'access_token=%s' % index()
        header = {
            'Content-Type':'application/json'
        }
        body = {
            'image': pic[23:],
            'image_type':'BASE64',
            'group_id':'diamond',
            'user_id':'xuer'

        }
        r=requests.post(url=url,data=body,headers=header)
        return HttpResponse(str(r.json()))
#用户信息
class yonghu(views.View):
    def get(self,request):
        # f = users()
        return render(request, 'yonghu.html')


    def post(self, request):
        # f = users(request.POST)
        # if f.is_valid():
        #     f.save()
        #     return HttpResponse('注册成功')
        # else:
        #     return HttpResponse('信息录入失败')
        name = request.POST.get('name')
        tel = request.POST.get("tel")
        sex = request.POST.get('sex')
        age = request.POST.get('age')
        money = request.POST.get('money')
        photo =request.POST.get("photo")
        if user.objects.filter(tel=tel).exists():
            return HttpResponse("手机号已被注册")
        else:
            a=user(name=name,tel=tel,sex=sex,age=age,money=money,photo=photo)
            a.save()
            # Women.objects.create(w_name='杨幂', w_age=20, w_money=10000, w_love='性格温和')
            a = Women.objects.all()
            return render(request,"limian.html",{"jishi":a})

def limian(request):
    a = Women.objects.all()
    return render(request,"limian.html",{"jishi":a})
#删除女生信息
def delete(request):
    if request.method.lower() =="post":
        pk=request.POST.get("id")
        Women.objects.filter(pk=pk).delete()
    return render(request,'delete.html')
#添加女生信息
def add(request):
    if request.method.lower() == "post":
        w_name=request.POST.get('w_name')
        w_age=request.POST.get("w_age")
        w_money=request.POST.get("w_money")
        w_love=request.POST.get("w_love")

        Women.objects.create(w_name=w_name,w_age=w_age,w_money=w_money,w_love=w_love)
    return render(request,"add.html")







